package droidninja.filepicker.fragments;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import java.io.File;

import droidninja.filepicker.PickerManager;
import droidninja.filepicker.load.LoadingDialog;
import top.zibin.luban.CompressionPredicate;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

/**
 * A simple {@link Fragment} subclass.
 */
public abstract class BaseFragment extends Fragment {

    public static final String FILE_TYPE="FILE_TYPE";

    @Nullable
    private LoadingDialog mProgressDialog;

    public BaseFragment() {
    }

    @NonNull
    protected LoadingDialog getProgressDialog() {
        if (mProgressDialog == null) {
            mProgressDialog = new LoadingDialog(new android.app.ProgressDialog(requireActivity()));
        }
        return mProgressDialog;
    }

    @Override
    public void onDestroyView() {
        if (mProgressDialog != null) {
            mProgressDialog.onDestroy();
        }
        super.onDestroyView();
    }

    /**
     * 显示加载进度
     */
    public void showLoading() {
        showLoading("正在加载...");
    }

    /**
     * 显示加载进度
     */
    public void showLoading(String title) {
        showLoading(title, true);
    }

    /**
     * 设置点击弹窗外面是否关闭弹窗，默认为true
     *
     * @param title 要显示的文字
     */
    public void showLoading(String title, boolean isDismissOutside) {
        showLoading(title, isDismissOutside, isDismissOutside);
    }

    public void showLoading(@StringRes int titleId) {
        showLoading(getResources().getString(titleId));
    }

    public void showLoading(@NonNull String title, boolean isDismissOnBackPressed,
                            boolean isDismissOnTouchOutside) {
        if (mProgressDialog == null) {
            getProgressDialog().showLoading(title, isDismissOnBackPressed, isDismissOnTouchOutside);
            return;
        }
        mProgressDialog.showLoading(title, isDismissOnBackPressed, isDismissOnTouchOutside);
    }

    public void hideLoading() {
        if (mProgressDialog == null) {
            return;
        }
        requireActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mProgressDialog == null) {
                    return;
                }
                mProgressDialog.hideLoading();
            }
        });
    }

    public int getLeastCompressSize() {
        return PickerManager.getInstance().getLeastCompressSize();
    }
}
